
$(function(){
    admin_ui_listing.init();
});




var admin_ui_listing = {
    m_sAction: '',
    m_aActions: {
        "create": false,
        "edit": false,
        "block": false,
        "unblock": false,
        "delete": false
    },
    
    setAction: function(str){
        this.m_sAction = str;
    },
    
    setActions: function(sType, sAction){
        this.m_aActions[sType] = sAction;
    },
    
    init: function(){
    
        $("#admin_ui_listing_action").val(this.m_sAction);
        
        $(".admin_ui_listing .row").hover(function(){
            $(this).addClass("hover");
        }, function(){
            $(this).removeClass("hover");
        });
        
        $(".admin_ui_listing .row:odd").addClass("odd");
        $(".admin_ui_listing .row:even").addClass("even");
        
        $("#admin_ui_listing_check_all").click(function(){
            if ($(this).attr("checked")) {
                $(".admin_ui_listing .id").attr("checked", true);
                $(".admin_ui_listing .row").addClass("checked");
            }
            else {
                $(".admin_ui_listing .id").attr("checked", false);
                $(".admin_ui_listing .row").removeClass("checked");
            }
            
            admin_ui_listing.update();
        });
        
        $(".admin_ui_listing .id").click(function(){
            $("#admin_ui_listing_check_all").attr("checked", $(".admin_ui_listing .id").length == $(".admin_ui_listing .id:checked").length);
            
            if ($(this).attr("checked")) 
                $("#admin_ui_listing_row_" + $(this).val()).addClass("checked");
            else 
                $("#admin_ui_listing_row_" + $(this).val()).removeClass("checked");
            
            admin_ui_listing.update();
        });
        
        
        $(".admin_ui_listing .id:checked").each(function(){
            $("#admin_ui_listing_row_" + $(this).val()).addClass("checked");
        });
        
        admin_ui_listing.update();
    },
    
    update: function(){
    
        $checkedID = $(".admin_ui_listing .id:checked");
        
        var aID = new Array();
        $checkedID.each(function(){
            aID.push($(this).val());
        });
        $("#admin_ui_listing_id").val(aID.join(","));
        
        var $edit = $("#admin_ui_listing_toolbar_edit"), $unblock = $("#admin_ui_listing_toolbar_unblock"), $block = $("#admin_ui_listing_toolbar_block"), $delete = $("#admin_ui_listing_toolbar_delete");
        
        var iLen = $checkedID.length;
        if (iLen == 0) {
            $edit.removeClass("able").addClass("disable");
            $unblock.removeClass("able").addClass("disable");
            $block.removeClass("able").addClass("disable");
            $delete.removeClass("able").addClass("disable");
        }
        else {
            $delete.removeClass("disable").addClass("able");
            if (iLen == 1) 
                $edit.removeClass("disable").addClass("able");
            else 
                $edit.removeClass("able").addClass("disable");
            
            if ($(".admin_ui_listing .checked .block").length > 0) 
                $unblock.removeClass("disable").addClass("able");
            else 
                $unblock.removeClass("able").addClass("disable");
            
            if ($(".admin_ui_listing .checked .unblock").length > 0) 
                $block.removeClass("disable").addClass("able");
            else 
                $block.removeClass("able").addClass("disable");
        }
    },
    
    create: function(){
        $("#admin_ui_listing_id").val("0");
        $("#admin_ui_listing_action").val(this.m_aActions['create']);
        $("#admin_ui_listing_form").submit();
    },
    
    block: function(id){
        if (id > 0) 
            $("#admin_ui_listing_id").val(id);
		$("#admin_ui_listing_action").val(this.m_aActions['block']);
        $("#admin_ui_listing_form").submit();
    },
    
    unblock: function(id){
        if (id > 0) 
            $("#admin_ui_listing_id").val(id);
		$("#admin_ui_listing_action").val(this.m_aActions['unblock']);
        $("#admin_ui_listing_form").submit();
    },
    
    edit: function(id){
        if (id > 0) 
            $("#admin_ui_listing_id").val(id);
		$("#admin_ui_listing_action").val(this.m_aActions['edit']);
        $("#admin_ui_listing_form").submit();
    },
    
    remove: function(id){
        if (!confirm("该操作不可恢复， 确认删除吗?")) 
            return false;
        if (id > 0) 
            $("#admin_ui_listing_id").val(id);
		$("#admin_ui_listing_action").val(this.m_aActions['delete']);
        $("#admin_ui_listing_form").submit();
    },
    
    orderBy: function(sField, sDir){
        $("#admin_ui_listing_id").remove();
        $("#admin_ui_listing_order_by").val(sField);
        $("#admin_ui_listing_order_by_dir").val(sDir);
        $("#admin_ui_listing_form").submit();
    },
    
    gotoPage: function(n){
        $("#admin_ui_listing_id").remove();
        $("#admin_ui_listing_page").val(n);
        $("#admin_ui_listing_action").val(this.m_sAction);
        $("#admin_ui_listing_form").submit();
    }
}

